import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-09-27
 * Time: 21:28
 */
public class MyStack {
    private int []elem;
    private int usedSize;
    public MyStack(){
        this.elem=new int[5];
    }
    public boolean push(int data){
        if(isFull()){
            this.elem= Arrays.copyOf(this.elem, 2*this.elem.length);
        }
        this.elem[usedSize]=data;
        usedSize++;
        return true;
    }

    public int pop(){
        if(isEmpty()){
            throw new RuntimeException("数组为空");
        }
        int val=this.elem[usedSize-1];//注意-1
        //如果数组元素是引用类型需要置为null
        usedSize--;
        return val;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("数组为空");
        }
        int val=this.elem[usedSize-1];//注意-1
        return val;
    }

    public boolean isEmpty(){
        return usedSize==0;
    }
    public boolean isFull(){
        return usedSize==elem.length;
    }
}
